/**
 * 
 */
package com.gf.pf.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gf.common.BaseController;
import com.gf.common.ResultMessage;
import com.gf.common.UUIDGenerator;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.model.Job;
import com.gf.pf.service.JobService;
import com.gf.pf.service.MyBatisService;

/**
 * @description 职位管理
 * @date   Mar 15, 2015
 */
@Controller
@RequestMapping("/pf/job")
public class JobController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(JobController.class);
	
	/**
	 * 职位列表
	 */
	private String JOB_LIST = "pf/jobList";
	
	/**
	 * 添加职位
	 */
	private String JOB_ADD = "pf/jobAdd";
	
	/**
	 * 修改职位
	 */
	private String JOB_MODIFY = "pf/jobModify";
	
	/**
	 * 每页记录数
	 */
	private String PAGE_SQL_ID = "pf.job.jobList";
	
	/**
	 * 总记录数
	 */
	private String TOTAL_SQL_ID = "pf.job.total";
	
	/**
	 * 职位ID为空！
	 */
	private String job_id_empty = "职位ID为空！";
	
	@Autowired
	private JobService jobService;

	@Autowired
	private MyBatisService myBatisService;
	
	/**
	 * 
	 * @description 职位管理列表
	 * @return String
	 */
	@RequestMapping(value = "/flJob", method = RequestMethod.GET)
	public String jobList() {
		return this.JOB_LIST;
	}
	
	/**
	 * 
	 * @description 职位管理列表数据
	 * @return Flexigrid
	 */
	@RequestMapping(value = "/dataGrid", method = RequestMethod.POST)
	@ResponseBody
	public Flexigrid dataGrid(Flexigrid flex) {
		flex.setRowStart((flex.getPage() - 1) * flex.getRp());
		flex.setRowEnd(flex.getPage() * flex.getRp());
		List<?> rows = myBatisService.getList(this.PAGE_SQL_ID, flex);
		int total = myBatisService.count(this.TOTAL_SQL_ID, flex);
		flex.setTotal(total);
		flex.setRows(rows);
		return flex;
	}
	
	/**
	 * 
	 * @description 添加职位跳转
	 * @return String
	 */
	@RequestMapping(value = "/faJob", method = RequestMethod.GET)
	public String jobAddForward() {
		return this.JOB_ADD;
	}
	
	/**
	 * 
	 * @description 添加职位
	 * @return String
	 */
	@RequestMapping(value = "/aJob", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage jobAdd(Job job) {
		job.setId(UUIDGenerator.getInstance().getUUID32());
		Integer result = jobService.saveJob(job);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("职位保存成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("职位保存失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 职位修改跳转
	 * @return String
	 */
	@RequestMapping(value = "/fmJob", method = RequestMethod.GET)
	public String jobModifyForward(String jobId, Model model) {
		if(!StringUtils.isEmpty(jobId)) {
			Job job = jobService.getJob(jobId);
			model.addAttribute("job", job);
		} else {
			LOGGER.error(this.job_id_empty);
			return this.JOB_LIST;
		}
		return this.JOB_MODIFY;
	}
	
	/**
	 * 
	 * @description 职位修改
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/mJob", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage jobModify(Job job) {
		if(StringUtils.isEmpty(job.getId())) {
			LOGGER.error(this.job_id_empty);
		}
		
		Integer result = jobService.modifyJob(job);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("职位修改成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("职位修改失败！");
		}
		return super.resultMessage;
	}
	
	/**
	 * 
	 * @description 删除职位
	 * @return ResultMessage
	 */
	@RequestMapping(value = "/dJob", method = RequestMethod.POST)
	@ResponseBody
	public ResultMessage jobDelete(String jobId) {
		if(StringUtils.isEmpty(jobId)) {
			LOGGER.error(this.job_id_empty);
		}
		
		Integer result = jobService.deleteJob(jobId);
		if(result > 0) {
			super.resultMessage.setFlag(super.RESULTMESSAGE_SUCCESS);
			super.resultMessage.setMsg("职位删除成功！");
		} else {
			super.resultMessage.setFlag(super.RESULTMESSAGE_FAIL);
			super.resultMessage.setMsg("职位删除失败！");
		}
		return super.resultMessage;
	}
}
